/* 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.shanekjohnson.cmis.client.atompub.service
{
	import org.shanekjohnson.cmis.client.atompub.model.Folder;
	import org.shanekjohnson.cmis.client.atompub.util.Encoder;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.filesystem.File;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	
	public class DocumentService extends EventDispatcher
	{
		private namespace app = "http://www.w3.org/2007/app";
		private namespace atom = "http://www.w3.org/2005/Atom";
		private namespace cmis = "http://docs.oasis-open.org/ns/cmis/core/200908/";
		
		use namespace app;
        use namespace atom;
        use namespace cmis;
        
		private var auth:String;
		
		public function DocumentService(auth:String)
		{
			this.auth = auth;
		}
		
		public function createDocument(folder:Folder, file:File, username:String):void 
		{
			var content:String = Encoder.getString(file);
			
			var msg:XML = 				
				<entry xmlns="http://www.w3.org/2005/Atom" xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200901">
				<id>http://willbeignoredbyserver</id>
					<title>{file.name}</title>
					<summary>{file.name}</summary>
				  <author>
				    <name>{username}</name>
				  </author>					
					<updated>2009-03-01T00:00:00.000Z</updated>
					<content type="application/octet-stream">{content}</content>
					<cmis:object>
						<cmis:properties>
							<cmis:propertyId cmis:name="ObjectTypeId" cmis:propertyType="String"><cmis:value>document</cmis:value></cmis:propertyId>
						      <cmis:propertyString cmis:name="Name" cmis:propertyType="String">
						        <cmis:value>Atom Item March-23 Try 1</cmis:value>
						      </cmis:propertyString>						
						</cmis:properties>
					</cmis:object>
				</entry>;
			
			var request:URLRequest = new URLRequest(folder.childrenURL);

			request.data = msg;
			request.contentType = "application/atom+xml;type=entry";
			request.method = URLRequestMethod.POST;
			
			var header:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + auth);
            
            request.requestHeaders.push(header);

			var loader:URLLoader = new URLLoader();

			loader.addEventListener(Event.COMPLETE, this.dispatchEvent);
			
			// for logging...
            loader.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
            loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleHttpStatus);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
            loader.addEventListener(Event.COMPLETE, handleComplete);

			loader.load(request);
		}

        private function handleIOError(event:IOErrorEvent):void
        {
            trace("Load failed: IO error: " + event.text);
        }
        
        private function handleHttpStatus(event:HTTPStatusEvent):void
        {
            trace("Load failed: HTTP Status = " + event.status);
        }
        
        private function handleSecurityError(event:SecurityErrorEvent):void
        {
            trace("Load failed: Security Error: " + event.text);
        }
        
        private function handleComplete(event:Event):void
        {
            trace("The data has successfully been loaded.");
        }
			
	}
	
}